package com.crm.controller.reportsController;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.crm.beans.EnqSummary;
import com.crm.beans.SearchVO;
import com.crm.dao.OpportunityDAO;
import com.crm.dao.PODAO;
import com.crm.dao.PartsDAO;
import com.crm.dao.QuotationDAO;
import com.crm.dao.SourceCompareDAO;
import com.crm.dao.SupplyOrderDAO;
import com.crm.entity.Business;
import com.crm.entity.Opportunity;
import com.crm.entity.PO;
import com.crm.entity.Parts;
import com.crm.entity.Quotation;
import com.crm.entity.Sourcecompare;
import com.crm.entity.SupplyOrder;
@Controller
@RequestMapping("/search")
public class SearchController {
		protected final Log logger = LogFactory.getLog(getClass());
		@RequestMapping("/show.do")
		public ModelAndView searchByPart(HttpServletRequest request,HttpServletResponse response,HttpSession session,ModelAndView model) throws Exception{
			Business bus=(Business)session.getAttribute("business");
			if(bus==null)return new ModelAndView("/jsp/index.jsp");
			return model=new ModelAndView("/jsp/se.jsp")
			.addObject("submenu", "reports")
			.addObject("menuName","reports");
			
		}
		@RequestMapping("/getByPart.do")
		public ModelAndView show(HttpServletRequest request,HttpServletResponse response,HttpSession session,ModelAndView model) throws Exception{
			Business bus=(Business)session.getAttribute("business");
			System.out.println("hello we re in getByPart.do .......................");
			if(bus==null)return new ModelAndView("/jsp/index.jsp");
			String partname=request.getParameter("partname");
			//OpportunityDAO oprDAO=new OpportunityDAO();
			List<EnqSummary> ls=OpportunityDAO.getSingleInstance().getEnquirySumm(partname,bus.getBusinessId());
			System.out.println("hello we re in getByPart.do .......................");
			return model=new ModelAndView("/jsp/searchByPartres.jsp")
			.addObject("submenu", "reports")
			.addObject("menuName","reports")
			.addObject("enqsum",ls);
			
		}
		@RequestMapping("/getByQuot.do")
		public ModelAndView showQuot(HttpServletRequest request,HttpServletResponse response,HttpSession session,ModelAndView model) throws Exception{
			Business bus=(Business)session.getAttribute("business");
			if(bus==null)return new ModelAndView("/jsp/index.jsp");
			String quotNo=request.getParameter("quot");
			String advancedSearch=request.getParameter("advancedSearch");
			String month=request.getParameter("month");
			String year=request.getParameter("year");
			System.out.println("Quotation Number Is : "+quotNo);
			QuotationDAO quotDAO=new QuotationDAO();
			List<Quotation> ls=new ArrayList<Quotation>();
			System.out.println("Advanced Search Value Is: "+advancedSearch);
				if(advancedSearch.equals("null")){
					System.out.println("Advanced Search Is null");
					ls=quotDAO.findByQuotName(bus.getBusinessId(),quotNo);
				}else{
					System.out.println("Month Number Is : "+month);
					System.out.println("Year Number Is : "+year);
					ls=quotDAO.findQuotByMonYear(bus.getBusinessId(),quotNo,month,year);
				}
			return model=new ModelAndView("/jsp/searchByQuotRes.jsp")
			.addObject("submenu", "reports")
			.addObject("menuName","reports")
			.addObject("allquot",ls);
			
		}
		@RequestMapping(value="/edit.do",method = RequestMethod.GET)
		public ModelAndView getSrchEnqTable(HttpServletRequest req,HttpSession session, ModelAndView model) throws Exception{
			Business bus=(Business)session.getAttribute("business");
			if(bus==null)return new ModelAndView("/jsp/index.jsp");
			String month=req.getParameter("mnth");
		String year=req.getParameter("year");
		logger.info("month is "+month);
		logger.info("year is "+year);
			//OpportunityDAO odao=new OpportunityDAO();
			List<Opportunity> enqs=null;
			try {
			
				enqs=OpportunityDAO.getSingleInstance().getenqs(month,year,bus.getBusinessId());
					
			} catch (Exception e1) {
			e1.printStackTrace();
			}
			model.setViewName("/jsp/srchEnqTable.jsp");
			model.addObject("srchEnqList",enqs);
			
			return model; 
		}
		@RequestMapping("/mainSearch.do")
		public ModelAndView mainSearch(HttpServletRequest request,HttpServletResponse response,HttpSession session,ModelAndView model) throws Exception{
			Business bus=(Business)session.getAttribute("business");
			if(bus==null)return new ModelAndView("/jsp/index.jsp");
			int busid=bus.getBusinessId();
			String partname=request.getParameter("partname");
			
			
			PartsDAO pdao=new PartsDAO();
			//OpportunityDAO oppdao=new OpportunityDAO();
			SupplyOrderDAO sodao=new SupplyOrderDAO();
			SourceCompareDAO scdao=new SourceCompareDAO();
			
			List<Parts> lp=pdao.findByPartName(partname, busid);
			System.out.println("The Size Of List From Parts Tab;le Is: "+lp.size());
			List<SearchVO> listopp=new ArrayList<SearchVO>();
			List<SearchVO> listquot=new ArrayList<SearchVO>();
			List<SearchVO> listpo=new ArrayList<SearchVO>();
			List<SearchVO> listvq=new ArrayList<SearchVO>();
			List<SearchVO> listinvoice=new ArrayList<SearchVO>();
			List<SearchVO> lsc=new ArrayList<SearchVO>();
			
			QuotationDAO qdao=new QuotationDAO();
			PODAO podao=new PODAO();
			
			
			int qty=0;
			
			for(int i=0;i<lp.size();i++){
				System.out.println("Enqid Is: "+lp.get(i).getEnqid());
				SearchVO svo=new SearchVO();
				Opportunity opp=OpportunityDAO.getSingleInstance().findById(lp.get(i).getEnqid(), busid);
				svo.setCustomerName(opp.getClientId().getShrtname());
				svo.setEnquiryNo(opp.getEnqnum());
				qty=pdao.findById(lp.get(i).getPartid(), busid,lp.get(i).getEnqid()).getQty();
				svo.setQuantity(qty);
				svo.setDate(opp.getCurr_date()+"");
				listopp.add(svo);
				
				
				Quotation q=qdao.getPendingQuot(opp.getId(), busid);
				if(q!=null){
					svo=new SearchVO();
					svo.setEnquiryNo(opp.getEnqnum());
					svo.setCustomerName(opp.getClientId().getShrtname());
					svo.setQuantity(lp.get(i).getQty());
					svo.setDate(opp.getCurr_date()+"");
					svo.setQuotePrice(q.getQuotvalue());
					listquot.add(svo);
				}
				
				PO po=podao.findbyEnqIdPartIdBusid(lp.get(i).getEnqid(), lp.get(i).getPartid(), busid);
				if(po!=null){
					svo=new SearchVO();
					svo.setEnquiryNo(opp.getEnqnum());
					svo.setCustomerName(opp.getClientId().getShrtname());
					svo.setQuantity(po.getQty());
					svo.setDate(po.getDate()+"");
					svo.setVendorPrice(Math.round(po.getSum()));
					listpo.add(svo);
				}
				SupplyOrder so=sodao.findByPartIdBusId(lp.get(i).getPartid(), busid);
				if(so!=null){
					svo=new SearchVO();
					svo.setEnquiryNo(opp.getEnqnum());
					svo.setCustomerName(opp.getClientId().getShrtname());
					svo.setQuantity(so.getQty());	
					svo.setDate(so.getDate()+"");
					svo.setVendorPrice(so.getPrice());
					listvq.add(svo);
					
					svo=new SearchVO();
					svo.setEnquiryNo(opp.getEnqnum());
					svo.setInvoiceNo(so.getInvoice());
					svo.setVendorPrice(so.getPrice());
					svo.setQuantity(so.getQty());
					svo.setDate(so.getDate()+"");
					svo.setCustomerName(opp.getClientId().getShrtname());
					listinvoice.add(svo);
				}
				Sourcecompare sc=scdao.findByPartIdBusid(opp.getId(), lp.get(i).getPartid(), busid);
				if(sc!=null){
					if(!lp.get(i).getPrtnum().equalsIgnoreCase(sc.getOfferingPartNo())){
						svo=new SearchVO();
						svo.setOfferingPartNo(sc.getOfferingPartNo());
						lsc.add(svo);
					}
				}
			}
			
			return model=new ModelAndView("/jsp/searchRes.jsp")
			.addObject("submenu", "reports")
			.addObject("menuName","reports")
			.addObject("opportunity",listopp)
			.addObject("quotation",listquot)
			.addObject("po",listpo)
			.addObject("vendorquote",listvq)
			.addObject("invoice",listinvoice)
			.addObject("offPart",lsc)
			;
		}
		@RequestMapping("/mainSearchShow.do")
		public ModelAndView mainSearchShow(HttpServletRequest request,HttpServletResponse response,HttpSession session,ModelAndView model) throws Exception{
			return model=new ModelAndView("/jsp/search.jsp")
			.addObject("submenu", "reports")
			.addObject("menuName","reports");
			
		}
		
	}

